rustdoc-search: search types by higher-order functions#119676
Merged
bors merged 4 commits intorust-lang:masterfrom Mar 14, 2024
Merged
rustdoc-search: search types by higher-order functions#119676bors merged 4 commits intorust-lang:masterfrom
bors merged 4 commits intorust-lang:masterfrom
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This feature extends rustdoc with syntax and search index information for searching function pointers and closures (Higher-Order Functions, or HOF). Part of #60485
This PR adds two syntaxes: a high-level one for finding any kind of HOF, and a direct implementation of the parenthesized path syntax that Rust itself uses.
Preview pages
option<T>, (fnonce (T) -> bool) -> option<T>Option::filteroption<T>, (T -> bool) -> option<T>Option::filterUpdated chapter of the book: https://notriddle.com/rustdoc-html-demo-9/search-hof/rustdoc/read-documentation/search.html
Motivation
When type-based search was first landed, it was directly described as incomplete.
Filling out the missing functionality is going to mean adding support for more of Rust's type expression syntax, such as references, raw pointers, function pointers, and closures. This PR adds function pointers and closures.
There's been demand for something "like Hoogle, but for Rust" expressed a few times 1 2 3 4. Some of them just don't realize what functionality already exists (
Duration -> u64already works), but a lot of them specifically want to search for higher-order functions like option combinators.Guide-level explanation (from the Rustdoc book)
To search for a function that accepts a function as a parameter, like
Iterator::all, wrap the nested signature in parenthesis, as inIterator<T>, (T -> bool) -> bool. You can also search for a specific closure trait, such asIterator<T>, (FnMut(T) -> bool) -> bool, but you need to know which one you want.Reference-level description (also from the Rustdoc book)
Primitives with Special Syntax
[]primitive:sliceand/orprimitive:array[T]primitive:slice<T>and/orprimitive:array<T>!primitive:never()primitive:unitand/orprimitive:tuple(T)T(T,)primitive:tuple<T>(T, U -> V, W)fn(T, U) -> (V, W), Fn, FnMut, and FnOnceThe
->operator has lower precedence than comma. If it's not wrapped in brackets, it delimits the return value for the function being searched for. To search for functions that take functions as parameters, use parenthesis.Search query grammar
Future direction
The remaining type expression grammar
As described in #118194, this is another step in the type expression grammar: BareFunction, and the function-like mode of TypePath, are now supported.
Search subtyping and traits
This is the other major factor that makes it less useful than it should be.
iterator<result<t>> -> result<t>doesn't findResult::from_iter. You have to searchintoiterator<result<t>> -> result<t>. Nobody's going to search for IntoIterator unless they basically already know about it and don't need the search engine anyway.Iterator combinators are usually structs that happen to implement Iterator, like
std::iter::Map.To solve these cases, it needs to look at trait implementations, knowing that Iterator is a "subtype of" IntoIterator, and Map is a "subtype of" Iterator, so
iterator -> resultis a subtype ofintoiterator -> resultanditerator<t>, (t -> u) -> iterator<u>is a subtype ofiterator<t>, (t -> u) -> map<t -> u>.